#include<iostream>
#include<algorithm>
#include<cstring>
//859
using namespace std;
const int N = 2e5 + 10;

int n, m;
int p[N];

struct Edge
{
    int a, b, w;

    bool operator< (const Edge& W) const{
        return w < W.w;
    }
    /* data */
}edge[N];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }

    return p[x];
}



int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    int res = 0;
    int cnt = 0;

    

    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i ++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        edge[i] = {a, b, c};
    }

    for(int i = 1; i <= n; i ++) p[i] = i;

    sort(edge, edge + m);

    for(int i = 0; i < m; i ++){
        int x = edge[i].a, y = edge[i].b, z = edge[i].w;
        if(find(x) != find(y)){
            p[find(x)] = find(y);
            res += z;
            cnt ++;
        }
    }

    if(cnt != n - 1){
        printf("impossible\n");
    }else{
        printf("%d\n", res);
    }

    return 0;

}